package com.mac.sys.urtils;


import com.mac.sys.generator.PropertiesHelper;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 *@Author:liangjilong
 *@Date:2015年12月5日下午12:25:12
 *@Email:jilongliang@sina.com
 *@Version:1.0
 *@CopyRight(c)Flong Intergrity Ltd.
 */
public class JDBCUtils
{
    private static final Connection conn;
    private static final String driverClass = "com.mysql.jdbc.Driver";
    //    public static  String connectionUrl = "jdbc:mysql://127.0.0.1:3306/macsite?useUnicode=true&characterEncoding=utf-8";
    public static String connectionUrl = "jdbc:mysql://129.226.48.135:43306/macsite?useUnicode=true&characterEncoding=utf-8";

    private static String username = "root";
    private static String password = "123456qgdb";

    private static JDBCUtils instance = null;

    /**
     * 定义代码块.
     */
    static
    {
        try
        {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(connectionUrl, username, password);
        } catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 建立单例模式
     * Single
     *
     * @return
     */
    public static JDBCUtils getInstance()
    {
        if (instance == null)
        {
            synchronized (JDBCUtils.class)
            {
                instance = new JDBCUtils();
            }
        }
        return instance;
    }


    /**
     * 查询数据
     *
     * @param sql
     * @param params
     * @return
     */
    public static ResultSet query(String sql, List<Object> params)
    {
        System.out.println("sql: " + sql);
        //System.out.println("params: " + params);
        try
        {
            PreparedStatement psmt = conn.prepareStatement(sql);
            if (params != null)
            {
                for (int i = 0; i < params.size(); i++)
                {
                    psmt.setObject(i + 1, params.get(i));
                }
            }
            return psmt.executeQuery();
        } catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
    }

    public static List queryList(String sql, List<Object> params)
    {
        List list = new ArrayList();
        System.out.println("sql: " + sql);
        //System.out.println("params: " + params);
        try
        {
            PreparedStatement psmt = conn.prepareStatement(sql);
            if (params != null)
            {
                for (int i = 0; i < params.size(); i++)
                {
                    psmt.setObject(i + 1, params.get(i));
                }
            }
            ResultSet rs =  psmt.executeQuery();

            ResultSetMetaData md = rs.getMetaData();//获取键名
            int columnCount = md.getColumnCount();//获取行的数量
            while (rs.next()) {
                Map rowData = new HashMap();//声明Map
                for (int i = 1; i <= columnCount; i++) {
                    rowData.put(md.getColumnName(i), rs.getObject(i));//获取键名及值
                }
                list.add(rowData);
            }
        } catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        return list;
    }

    public static boolean insert(String sql, List<Object> params)
    {
        System.out.println("sql: " + sql);
        //System.out.println("params: " + params);
        try
        {
            PreparedStatement psmt = conn.prepareStatement(sql);
            if (params != null)
            {
                for (int i = 0; i < params.size(); i++)
                {
                    psmt.setObject(i + 1, params.get(i));
                }
            }

            psmt.execute();

        } catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        return true;
    }

    /***
     * 更新
     * @param sql
     * @param params
     */
    public static void update(String sql, List<Object> params)
    {
        System.out.println("sql: " + sql);
        //System.out.println("params: " + params);
        try
        {
            PreparedStatement psmt = conn.prepareStatement(sql);
            if (params != null)
            {
                for (int i = 0; i < params.size(); i++)
                {
                    psmt.setObject(i + 1, params.get(i));
                }
            }
            psmt.executeUpdate();
        } catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取链接
     *
     * @return
     */
    public static Connection getConnection()
    {
        try
        {
            Class.forName(driverClass);
            return DriverManager.getConnection(connectionUrl, username, password);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


}
